const app = getApp()
// const user = wx.getStorageSync("user");
var user_token = wx.getStorageSync('user_token');
const base_url = app.globalData.base_url;
var id = 0;
const util = require('../../utils/util.js');
Page({
  data: {
    phone: null,
    index: 0,
    item1: ["京", "沪", "浙", "苏", "粤", "鲁", "晋", "冀",
      "豫", "川", "渝", "辽", "吉", "黑", "皖", "鄂",
      "津", "贵", "云", "桂", "琼", "青", "新", "藏",
      "蒙", "宁", "甘", "陕", "闽", "赣", "湘"],
    item2: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
      "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P",
      "A", "S", "D", "F", "G", "H", "J", "K", "L",
      "Z", "X", "C", "V", "B", "N", "M"],
    hidden1: true,
    hidden2: true,
    carFlag: false,
    carNo: '',
    formData: {},
    car_str: '浙',
    is_first: false,
  },
  listenerPickerSelected: function (e) {
    //改变index值，通过setData()方法重绘界面
    this.setData({
      index: e.detail.value,
      car_str: this.data.sheng[e.detail.value]
    });
  },

  onLoad: function (options) {
    id = decodeURIComponent(options.id);

    wx.request({
      url: base_url + '/CarUsers/userInfo',
      method: 'POST',
      data: {
        'user_token': JSON.parse(wx.getStorageSync("user")).user_token
        // 'user_token': '6e08f3ffb6c3f09d4c7ec14ad20eb70c'
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res.data.data)
        if (res.data.success == '1') {
          if (res.data.data.phone == null) {
            this.setData({
              is_first: true
            })
          }

        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })

  },
  selCarStr: function () {//打开省选择器
    this.setData({ carFlag: !this.data.carFlag })
    var that = this;
    that.setData({
      hidden1: false,
      hidden2: true
    })

  },
  selCarNo: function () {//打开车牌选择器
    this.setData({ carFlag: !this.data.carFlag })
    var that = this;

    that.setData({
      hidden1: true,
      hidden2: false
    })
  },
  //车牌输入获取焦点  
  d1: function () {
    var that = this;
    if (that.data.carNo == '') {
      that.setData({
        hidden1: false,
        hidden2: true
      })
    } else {
      that.setData({
        hidden1: true,
        hidden2: false
      })
    }

  },
  //车牌输入失去焦点  
  d2: function () {
    var that = this;
    that.setData({
      hidden1: true,
      hidden2: true
    })
  },
  //获取车牌省份  
  sheng: function (e) {
    var that = this;
    console.log(e.currentTarget.dataset.sh);
    that.setData({
      // carNo: e.currentTarget.dataset.sh,
      car_str: e.currentTarget.dataset.sh,
      hidden1: true,
      hidden2: false
    })
    // this.setData({ carFlag: !this.data.carFlag })

  },
  //获取车牌号码  
  other: function (e) {
    var that = this;
    console.log(e.currentTarget.dataset.ot);
    if (that.data.carNo.length < 7) {
      var carNo = that.data.carNo + e.currentTarget.dataset.ot;
      that.setData({
        carNo: carNo
      })
    }

  },
  //回删车牌  
  del: function () {
    var that = this;
    var ss = that.data.carNo;
    console.log(ss);
    var s = ss.split('');
    console.log(s);
    console.log(s.slice(0, -1));
    // if (s.slice(0, -1).length == 0) {
    //   that.setData({
    //     hidden1: false,
    //     hidden2: true
    //   })
    // }
    console.log(s.join('').slice(0, -1));
    var s = s.join('').slice(0, -1);
    that.setData({
      carNo: s
    })
    console.log(that.data.carNo.length);

  },
  //确认输入  
  ok: function () {
    var that = this;
    that.setData({
      hidden1: true,
      hidden2: true
    })
  },

  getPhone: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  commit: function () {
    if (this.data.carNo !== null && this.data.carNo !== '') {
      if (this.data.car_str == null) {
        wx.showToast({
          title: '请选择车牌前缀',
          icon: 'none',
          duration: 2000
        })
        return
      }
      var re = new RegExp('^[A-Za-z0-9]{7}|[A-Za-z0-9]{6}$');
      if (!re.exec(this.data.carNo)) {
        wx.showToast({
          title: '车牌号格式不正确',
          icon: 'none',
          duration: 2000
        })
        return;
      }
    }
   
    if (this.data.is_first) {
      if (this.data.phone == null) {
        wx.showToast({
          title: '请输入挪车电话',
          icon: 'none',
          duration: 2000
        })
        return
      } else {
        var myreg = new RegExp("^1[3456789][0-9]{9}$");

        if (!myreg.exec(this.data.phone)) {
          wx.showToast({
            title: '手机号格式不正确',
            icon: 'none',
            duration: 2000
          })
          return;
        }
      }
      this.setData({ 'formData.phone': this.data.phone })
    }
    this.setData({
      'formData.user_token': JSON.parse(wx.getStorageSync("user")).user_token,
      'formData.id':id,
      'formData.car_str': this.data.car_str,
      'formData.car_number': this.data.carNo
    })

    let data = this.data.formData;

    wx.request({
      url: base_url + '/Qcode/add',
      method: 'POST',
      data: data,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        if (res.data.success == '1') {
          if(res.data.code=='2'){
            wx.redirectTo({
              url: '../myInfo/index'
            })
          }else{
            wx.redirectTo({
              url: '../bindInfo/index?id=' + id
            })
          }
         
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }

      }
    })
  },

})
